package com.sk.weichat.adapter;

import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.weichat.MyApplication;
import com.sk.weichat.R;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.bean.User;
import com.sk.weichat.bean.message.ChatMessage;
import com.sk.weichat.bean.message.XmppMessage;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.util.DateUtils;
import com.sk.weichat.util.DisplayUtil;
import com.sk.weichat.util.HtmlUtils;
import com.sk.weichat.util.StringUtils;
import com.sk.weichat.util.TimeUtils;
import com.sk.weichat.view.HeadView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 搜索陌生人列表适配器
 * create by swy 2019-07-15
 */
public class SearchFriendAdapter extends BaseQuickAdapter<User, BaseViewHolder> {


    public SearchFriendAdapter(List<User>userList) {
        super(R.layout.item_search_friend, userList);

    }

    @Override
    protected void convert(BaseViewHolder helper, User item) {

        helper.addOnClickListener(R.id.layout_search_friend_item)
        .addOnClickListener(R.id.layout_search_attention_btn);
        helper.setText(R.id.tv_search_nickname,item.getNickName());

        ImageView imgAvatar = helper.getView(R.id.img_search_avatar);

        CheckBox checkBoxSex=helper.getView(R.id.checkbox_search_sex);//性别、年龄
        TextView tvAnonymity=helper.getView(R.id.tv_search_anonymity);//匿名或显示距离、登录时间
        RelativeLayout layoutAttention=helper.getView(R.id.layout_search_attention_btn);
        TextView tvAttention=helper.getView(R.id.tv_search_attention);
        ImageView imgAttention=helper.getView(R.id.img_search_attention);


        //距离、登录时间
        double latitude = MyApplication.getInstance().getBdLocationHelper().getLatitude();
        double longitude = MyApplication.getInstance().getBdLocationHelper().getLongitude();
        String distance = DisplayUtil.getDistanceInfo(latitude, longitude, item.getLoc());
        if(item.getSettings()==null){
            tvAnonymity.setVisibility(View.INVISIBLE);
        }else if(item.getSettings().getShowLastLoginTime()==1 && item.getSettings().getAllowQueryLoc()==1){//显示登录时间，距离
            if(TextUtils.isEmpty(distance)){
                tvAnonymity.setText(TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), item.getShowLastLoginTime()));
            }else{
                tvAnonymity.setText(distance+" · "+ TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), item.getShowLastLoginTime()));
            }
        }else if(item.getSettings().getShowLastLoginTime()==1){
            tvAnonymity.setText(TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), item.getShowLastLoginTime()));//只显示登录时间
        }else if(item.getSettings().getAllowQueryLoc()==1){//只显示距离
            if(TextUtils.isEmpty(distance)){
                tvAnonymity.setVisibility(View.INVISIBLE);
            }else{
                tvAnonymity.setText(distance);
            }
        }else {
            tvAnonymity.setText("隐身");
        }

        //性别
        if(item.getSex()== 0){//女
            checkBoxSex.setChecked(false);
        }else{
            checkBoxSex.setChecked(true);
        }
        checkBoxSex.setText(DateUtils.getAge(new Date(item.getBirthday()*1000))+"");
        //头像
        AvatarHelper.getInstance().displayAvatar("", item.getUserId(),imgAvatar, true);

        //关注
        if(item.getFriends()!=null
                &&(item.getFriends().getStatus()==Friend.STATUS_ATTENTION
                ||item.getFriends().getStatus()==Friend.STATUS_FRIEND)){
            layoutAttention.setBackgroundResource(R.drawable.bg_border_btn_yellow);
            tvAttention.setTextColor(ContextCompat.getColor(mContext,R.color.color_FCC23C));
            tvAttention.setText("已关注");
            imgAttention.setVisibility(View.GONE);

        }else{
            layoutAttention.setBackgroundResource(R.drawable.bg_btn_msg_attention);
            tvAttention.setTextColor(ContextCompat.getColor(mContext,R.color.app_white));
            tvAttention.setText("关注");
            imgAttention.setVisibility(View.VISIBLE);
        }
    }
}